import requests
from lxml import etree

from utils.YdmVerify import YdmVerify

url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
# 2. UA伪装: 将对应的User-Agent封装到一个字典中
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
}

page_text = requests.get(url=url,headers=headers).text

tree = etree.HTML(page_text)

img_path = tree.xpath('//img[@id="imgCode"]/@src')

code_img_src = 'https://so.gushiwen.cn/'+img_path[0]

img_data = requests.get(code_img_src,headers=headers).content

with open('验证码.jpg','wb') as f:
    f.write(img_data)

# 识别验证码
y = YdmVerify()
img_data_encoded= open('验证码.jpg', 'rb').read()
code = y.common_verify(image=img_data_encoded,verify_type='10110')

print(code)